package client

import (
	"context"
	"github.com/golang/protobuf/ptypes/wrappers"
	"github.com/micro/go-micro/v2/client"
	"github.com/micro/go-micro/v2/client/grpc"

	"kop/framework/wrapper"
	"kop/pb"
	"kop/pb/service"
)

var CaptainService service.CaptainService

func init() {

	var c = grpc.NewClient(client.Wrap(wrapper.ClientLogWrapper))
	_ = c.Init()

	//s.Init(test.Flags()...)

	CaptainService = service.NewCaptainService("go.micro.srv.captain", c)
}

//func Piece(roleID int32, item *pb.Item) (*pb.CaptainPieceResp, error) {
//
//	var resp, err = CaptainService.Piece(context.Background(), &wr.PieceRequest{RoleID: roleID, Item: item})
//	if err == nil {
//		*item = *resp.Item
//	}
//
//	return resp, err
//}

func CreateCaptain(ctx context.Context, captainID int32) *pb.CreateCaptainResp {

	var resp, err = CaptainService.Create(ctx, &wrappers.Int32Value{Value: captainID})
	if err != nil {
		panic(err)
	}

	return resp
}

func GetCaptains(roleID int32) []*pb.Captain {

	var resp, err = CaptainService.GetCaptains(context.Background(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}
	return resp.Captains
}

func SumPower(roleID int32) int64 {

	var captains = GetCaptains(roleID)
	var power int64
	for _, v := range captains {
		power += v.Power
	}

	return power
}

// 合成魔戒时检查是否有装备在船长身上
func GetByInRingUniqueIDs(roleID int32) []int32 {

	var resp, err = CaptainService.GetRingUniqueIDs(context.Background(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}
	return resp.Slices
}

func GetCaptainsByReady(roleID int32) []*pb.Captain {

	var resp, err = CaptainService.GetCaptainsByReady(context.Background(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}
	return resp.Captains
}

func GetCaptain(roleID, captainID int32) *pb.Captain {

	var captains = GetCaptains(roleID)
	for _, captain := range captains {
		if captain.CaptainID == captainID {
			return captain
		}
	}

	panic("can't found captain")
}

func SkillLevelUp(roleID, captainID, index int32) (*pb.Captain, error) {

	return CaptainService.SkillLevelUp(context.Background(), &service.CaptainRequest{RoleID: roleID, CaptainID: captainID, Value: index})
}

func AddExp(roleID, captainID, exp int32) (*pb.Captain, error) {

	return CaptainService.AddExp(context.Background(), &service.CaptainRequest{RoleID: roleID, CaptainID: captainID, Value: exp})
}

//func Equip(ctx, captainID int32, ring *pb.Ring) (*pb.Captain, error) {
//
//	return CaptainService.Equip(context.Background(), &service.CaptainEquipRequest{CaptainID: captainID, Ring: ring})
//}

func Ready(roleID, captainID int32, ready bool) (*pb.Captain, error) {

	return CaptainService.Ready(context.Background(), &service.CaptainReadyRequest{RoleID: roleID, CaptainID: captainID, Ready: ready})
}

//func AsyncSaveMaid(maid *pb.Maid) {
//
//	_, _ = CaptainService.AsyncSaveMaid(context.Background(), maid)
//}

//func AsyncSaveRing(roleID int32, ring *pb.Ring) {
//
//	_, _ = CaptainService.AsyncSaveRing(context.Background(), &service.CaptainEquipRequest{RoleID: roleID, Ring: ring})
//}

func SetBuildingID(roleID, captainID, buildingID int32) {

	var _, err = CaptainService.SetBuildingID(context.Background(), &service.SetBuildingIDRequest{RoleID: roleID, CaptainID: captainID, BuildingID: buildingID})
	if err != nil {
		panic(err)
	}
}

func GetTrainSeats(roleID int32) *pb.TrainSeats {

	var resp, err = CaptainService.GetTrainSeats(context.Background(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}
	return resp
}

func TrainUnlock(roleID, seat int32) *pb.TrainSeat {

	var resp, err = CaptainService.TrainUnlock(context.Background(), &service.DefaultRequest{RoleID: roleID, Value: seat})
	if err != nil {
		panic(err)
	}
	return resp
}

//func Train(roleID, captainID, seat int32) (*pb.TrainSeat, error) {
//
//	return CaptainService.Train(context.Background(), &service.TrainRequest{RoleID: roleID, CaptainID: captainID, Seat: seat})
//}
//
//func TrainFinish(roleID, seat int32) (*service.TrainResponse, error) {
//
//	return CaptainService.TrainFinish(context.Background(), &service.TrainRequest{RoleID: roleID, Seat: seat})
//}

//func CountByLevel(roleID, level int32) int32 {
//
//	var n, err = CaptainService.CountByLevel(context.Background(), &service.DefaultRequest{RoleID: roleID, Value: level})
//	if err != nil {
//		panic(err)
//	}
//
//	return n.Value
//}
//
//func CountByClass(roleID, class int32) int32 {
//
//	var n, err = CaptainService.CountByClass(context.Background(), &service.DefaultRequest{RoleID: roleID, Value: class})
//	if err != nil {
//		panic(err)
//	}
//
//	return n.Value
//}

//func Maid(roleID, captainID, maidID int32) (*pb.Captain, error) {
//
//	return CaptainService.Maid(context.Background(), &service.CaptainMaidRequest{RoleID: roleID, CaptainID: captainID, MaidID: maidID})
//}

func GetByCaptainIDs(roleID int32, captainIDs ...int32) []*pb.Captain {

	var captains = GetCaptains(roleID)

	var res []*pb.Captain
	for _, captain := range captains {
		for _, captainID := range captainIDs {
			if captain.CaptainID == captainID {
				res = append(res, captain)
			}
		}
	}

	return res
}
